package org.kamionowski.supreme.jobs;

import org.apache.commons.lang.Validate;
import org.kamionowski.supreme.services.crawler.Crawler;
import org.kamionowski.supreme.services.crawler.exc.CrawlerException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.concurrent.Future;

/**
 * Created by IntelliJ IDEA.
 * User: Marcin Kamionowski
 * Date: 11.04.11
 * Time: 23:46
 */
@Component
public class CrawlerJobImpl implements CrawlerJob {

    @Inject
    private Crawler crawler;

    @Override
    @Async
    public Future<Boolean> doScanning(ScanContext context){
        Validate.notNull(context);
        try{
            crawler.scanService(context.getRequest().getServiceToScan().getId());
            return new AsyncResult<Boolean>(true);
        }catch (CrawlerException exc){
            return new AsyncResult<Boolean>(false);
        }catch (Exception exc){
            return new AsyncResult<Boolean>(false);
        }finally {
            context.setEnded(true);
        }
    }
}
